﻿using BaseData.CommonLib.Models;
using FluentValidation;

namespace BaseData.CommonLib.Validators
{
    public class CalculateUnitEditValidator : AbstractValidator<CalculateUnitEditRequestDto>
    {

        public CalculateUnitEditValidator()
        {
            RuleFor(i => i.Code).NotEmpty().WithMessage("计量单位编码不能为空");
            RuleFor(i => i.Name).NotEmpty().WithMessage("计量单位名称不能为空");
        }

        public Func<object, string, Task<IEnumerable<string>>> ValidateValue => async (model, propertyName) =>
        {
            var result = await ValidateAsync(ValidationContext<CalculateUnitEditRequestDto>.CreateWithOptions((CalculateUnitEditRequestDto)model, x => x.IncludeProperties(propertyName)));
            if (result.IsValid)
                return Array.Empty<string>();
            return result.Errors.Select(e => e.ErrorMessage);
        };
    }
}